import 'package:bigegg/entity/user/xmm_wall_user.dart';
import 'package:bigegg/global/user/xmm_account_manager.dart';
import 'package:get/get.dart';

import '../../../../common/get/controller/xmm_base_controller.dart';
import '../../../../entity/xmm_video_info.dart';
import '../../../../global/xmm_app_const.dart';
import '../../../../utils/xmm_event_bus.dart';

class XmmVideoController extends XmmBaseGetxController {
  final topUserList = <XmmWallUser>[].obs;
  final hotVideoList = <XmmVideoInfo>[].obs;

  @override
  void onReady() {
    super.onReady();
    XmmEventBus.instance.on(XmmAppConst.eventBlockStatusUpdateKey, (arg) => getVideoList());
  }

  @override
  void onClose() {
    XmmEventBus.instance.off(XmmAppConst.eventBlockStatusUpdateKey);
    super.onClose();
  }

  void getVideoList() {
    ///视频列表
    var userList = XmmAccountManager.instance.wallUserList
        .where((element) => element.isBlack != true); //不展示被拉黑的。
    var hotList = <XmmVideoInfo>[];
    for (var element in userList) {
      var videoList = XmmAccountManager.instance.findVideo(element.userId ?? "");
      hotList.addAll(videoList);
    }
    hotVideoList.value = hotList;
    hotVideoList.refresh();

    ///顶部用户列表
    if(hotVideoList.isNotEmpty) {
      if (userList.length > 5) {
        userList = userList.take(5);
      }
      topUserList.value = userList.toList();
      topUserList.refresh();
    } else {
      topUserList.clear();
      topUserList.refresh();
    }
  }
}
